/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catosist.run.business.gestorcliente;

import com.catosist.run.dataaccess.domain.ClienteDao;
import com.catosist.run.dataaccess.model.ClienteVO;
import com.catosist.run.services.ClienteDTO;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author igorov
 */
public class GestorClienteImpl implements GestorCliente{

    private ClienteDao clienteDao;
    private List<ClienteVO> clienteVOs;

    public void deleteById(int i) {
        clienteDao.deleteById(i);
    }

    public void deleteByIndex(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ClienteDTO> getAll() {
//        if (clienteVOs == null) {
            updateListVo();
//        }

        List<ClienteDTO> clienteDTOs = new LinkedList<ClienteDTO>();
        for (Iterator<ClienteVO> it = clienteVOs.iterator(); it.hasNext();) {
            ClienteVO clienteVO = it.next();
            clienteDTOs.add(new ClienteDTO(
                    clienteVO.getId(),clienteVO.getNombre(),
                    clienteVO.getDireccion(), clienteVO.getRuc()));
        }
        return clienteDTOs;
    }

    public ClienteDTO getById(int voId) {
        ClienteVO clienteVO = clienteDao.get(voId);
        ClienteDTO clienteDTO = new ClienteDTO(
                clienteVO.getId(),
                clienteVO.getNombre(),
                clienteVO.getDireccion(),
                clienteVO.getRuc());

        return clienteDTO;
    }

    public ClienteDTO getByIndex(int indice) {
        ClienteVO clienteVO = getVOatIndex(indice);
        return new ClienteDTO(clienteVO.getId(), clienteVO.getNombre(),
                                clienteVO.getDireccion(), clienteVO.getRuc());
    }

    public ClienteVO getVOatIndex(int indice) {
//        return clienteDao.get(indice);
        System.out.println("Indice: "+indice);
        return clienteVOs.get(indice);
    }

    public int getIdAtIndex(int indice) {
        System.out.println("Indice: " + indice);
        return getVOatIndex(indice).getId();
    }

    public int getIndexOf(int voId) {
        updateListVo();
        ClienteVO clienteVO = clienteDao.get(voId);
        return clienteVOs.indexOf(clienteVO);
    }

    public void saveOrUpdate(ClienteDTO o) {
        ClienteVO clienteVO = new ClienteVO();
        if (o.getId() != 0) {
            clienteVO.setId(o.getId());
        }
        clienteVO.setNombre(o.getNombre());
        clienteVO.setDireccion(o.getDireccion());
        clienteVO.setRuc(o.getRUC());

        clienteDao.saveOrUpdate(clienteVO);
    }

    public ClienteDao getClienteDao() {
        return clienteDao;
    }

    public void setClienteDao(ClienteDao clienteDao) {
        this.clienteDao = clienteDao;
    }

    

    public void updateListVo() {
        clienteVOs = clienteDao.getAll();
    }

}
